package com.sword.controller.stock;

import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.page.Result;
import com.sword.pojo.stock.Backsale;
import com.sword.pojo.stock.BacksaleDetail;
import com.sword.service.impl.stock.BackSaleDetailServiceImpl;
import com.sword.service.impl.stock.BackSaleServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/stock")
public class BackSaleController {
    @Autowired
    private BackSaleServiceImpl backSaleService;
    @Autowired
    private BackSaleDetailServiceImpl backSaleDetailService;

    @RequestMapping("/backsale")
    public String backsale() {
        return "views/stock/backSale";
    }
    @RequestMapping("/backsaledetil")
    public String backsaledetail() {
        return "views/stock/backSaleDetail";
    }

    /**
查询所有退货单
*/
    @RequestMapping("/getBacksales")
    @ResponseBody
    public PageResult queryBacksales(@RequestBody QueryPageBean queryPageBean) {
        try {
            return backSaleService.queryBackSale(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_BACKSALE_FAIL, null, null);
        }
    }
    /**
     * 添加退货单功能
     */

    @ResponseBody
    @RequestMapping("/addBacksale")
    public Result addBacksale(@RequestBody Backsale backSale) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            backSale.setEmployeeid(userId);
            // 判断是否添加成功
            Integer flag = backSaleService.addBackSale(backSale);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_BACKSALE_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_BACKSALE_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_BACKSALE_SUCCESS, null);
    }
    /**
     * 修改退货单
     */
    @ResponseBody
    @RequestMapping("editBacksale")
    public Result editBacksale(@RequestBody Backsale backSale) {
        try {
            Integer flag = backSaleService.editBackSale(backSale);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BACKSALE_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BACKSALE_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BACKSALE_SUCCESS, null);
    }

    /**
     * 删除退货单
     */

    @ResponseBody
    @RequestMapping("deleteBacksale")
    public Result deleteBacksale(Integer id) {
        try {
            Integer flag = backSaleService.deleteBackSale(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_BACKSALE_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_BACKSALE_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_BACKSALE_SUCCESS, null);
    }






    /**
查询所有退货明细
*/
    @RequestMapping("/getBacksaledetails")
    @ResponseBody
    public PageResult queryBacksaledetails(@RequestBody QueryPageBean queryPageBean) {
        try {
            return backSaleDetailService.queryBackSaleDetail(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_BACKSALEDETAIL_FAIL, null, null);
        }
    }
    /**
     * 添加退货明细功能
     */

    @ResponseBody
    @RequestMapping("/addBacksaledetail")
    public Result addBacksaledetail(@RequestBody BacksaleDetail backSaleDetail) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            backSaleDetail.setCreateid(userId);
            // 判断是否添加成功
            Integer flag = backSaleDetailService.addBackSaleDetail(backSaleDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_BACKSALEDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_BACKSALEDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_BACKSALEDETAIL_SUCCESS, null);
    }
    /**
     * 修改退货明细
     */
    @ResponseBody
    @RequestMapping("editBacksaledetail")
    public Result editBacksaledetail(@RequestBody BacksaleDetail backSaleDetail) {
        try {
            Integer flag = backSaleDetailService.editBackSaleDetail(backSaleDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BACKSALEDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BACKSALEDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BACKSALEDETAIL_SUCCESS, null);
    }

    /**
     * 审核退货明细
     */
    @ResponseBody
    @RequestMapping("examineBacksaledetail")
    public Result examineBacksaledetail(@RequestBody BacksaleDetail backSaleDetail) {
        try {
            Integer flag = backSaleDetailService.examineBackSaleDetail(backSaleDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BACKSALEDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BACKSALEDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BACKSALEDETAIL_SUCCESS, null);
    }
    /**
     * 弃审退货明细
     */
    @ResponseBody
    @RequestMapping("unexamineBacksaledetail")
    public Result unexamineBacksaledetail(@RequestBody BacksaleDetail backSaleDetail) {
        try {
            Integer flag = backSaleDetailService.unexamineBackSaleDetail(backSaleDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_BACKSALEDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_BACKSALEDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_BACKSALEDETAIL_SUCCESS, null);
    }
    /**
     * 删除退货明细
     */

    @ResponseBody
    @RequestMapping("deleteBacksaledetail")
    public Result deleteBacksaledetail(Integer id) {
        try {
            Integer flag = backSaleDetailService.deleteBackSaleDetail(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_BACKSALEDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_BACKSALEDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_BACKSALE_SUCCESS, null);
    }
}
